/**
 * @param {*} date 传入的时间
 * @description 获取标准时间字符串 如: 2019-08-02
 */
const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

/**
 * @param {*} date 传入的日期 如: 2019-08-02
 * @description 根据年月日计算年龄
 */
const countAge = date => {
  let r = date.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);
  if (r == null) return false;
  let d = new Date(r[1], r[3] - 1, r[4]);
  if (d.getFullYear() == r[1] && (d.getMonth() + 1) == r[3] && d.getDate() == r[4]) {
    let Y = new Date().getFullYear();
    let age = Y - r[1];
    if (age < 0) age = 0;
    return age;
  }
}

/**
 * @param {*} that 小程序this
 * @param {*} count 倒计时数值
 * @description 倒计时60秒
 */
const countDown = (that, count) => {
  if (count === 0) {
    that.setData({
      timeCountDownTop: '获取验证码',
      counting: false
    })
    return;
  }

  that.setData({
    counting: true,
    timeCountDownTop: count + '秒后重新获取'
  })
  
  setTimeout(function(){
    count--;
    countDown(that, count);
  }, 1000);
}

//时间转化时间戳
const Timestamp = time =>{
	if(time === null){
		return
	}
	let timer = this.getLocalTime(time);//格式转换
	var thisTime = timer.replace(/-/g, '/');
	var timeVal = new Date(thisTime);
	timeVal = timeVal.getTime();
	return timeVal
}
const add0 = m => { return m < 10 ? '0' + m:m }
// 时间格式转换
 const getLocalTime= val => {
	if(val === null){
		return
	}
	// var _this=this
	// var time = new Date(val * 1000)
   var time;
   if (val.toString().length <= 10) {//秒
     time = new Date(val * 1000)//秒
   } else {
     time = new Date(val)//秒
   }
	var y = time.getFullYear()
	var m = time.getMonth() + 1
	var d = time.getDate()
	var h = time.getHours()
	var mm = time.getMinutes()
	var s = time.getSeconds()
	return y+'-'+add0(m)+'-'+add0(d)+' '+add0(h)+':'+add0(mm)+':'+add0(s);

}
// 时间格式年月日
 const getLocalDate = val => {
	 // console.log(val)
	if(val === null){
		return
	}
	var time = new Date(val)
	var y = time.getFullYear()
	var m = time.getMonth() + 1
	var d = time.getDate()
	// console.log(y,m,d)
	return y+'年'+m+'月'+d+'日';
}
const getLocalDate1 = val => {
	 // console.log(val)
	if(val === null){
		return
	}
	var time = new Date(val*1000)
	var y = time.getFullYear()
	var m = time.getMonth() + 1
	var d = time.getDate()
	
	return y+'年'+m+'月'+d+'日';
}

const formatRichText = html =>{
    let newContent= html.replace(/<img[^>]*>/gi,function(match,capture){
            match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
            match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
            match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
            return match;
        });
        newContent = newContent.replace(/style="[^"]+"/gi,function(match,capture){
            match = match.replace(/width:[^;]+;/gi, 'max-width:100%;').replace(/width:[^;]+;/gi, 'max-width:100%;');
            return match;
        });
        newContent = newContent.replace(/<br[^>]*\/>/gi, '');
        newContent = newContent.replace(/\<img/gi, '<img style="max-width:100%;height:auto;display:block;margin-top:0;margin-bottom:0;"');
        return newContent;
}

module.exports = {
  formatTime,
  countAge, add0, formatRichText,
  countDown,Timestamp,getLocalTime,getLocalDate,getLocalDate1
}
